package com.luoxiang.socialandroid.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Friend;

import java.util.List;

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {

    private List<Friend> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    //    public FriendAdapter(List<Friend> list) {
//        this.list = list;
//    }
    public void setData(List<Friend> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int userid); //传递boolean类型数据给activity
    }

    public static interface OnAddClickListenerDs {
        // true add; false cancel
        void onItemClicksDs(int userid);
    }

    public static interface OnAddClickListenerSx {

        void onItemClicksSx(int userid);
    }

    public static interface OnAddClickListenerSp {

        void onItemClicksSp(int userid);
    }

    OnAddClickListener onItemAddClick;

    OnAddClickListenerDs onItemAddClickDs;

    OnAddClickListenerSx onItemAddClickSx;

    OnAddClickListenerSp onAddClickListenerSp;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public void setOnAddClickListenerDs(OnAddClickListenerDs onItemAddClickDs) {
        this.onItemAddClickDs = onItemAddClickDs;
    }

    public void setOnAddClickListenerSx(OnAddClickListenerSx onItemAddClickSx) {
        this.onItemAddClickSx = onItemAddClickSx;
    }

    public void setOnAddClickListenerSp(OnAddClickListenerSp onAddClickListenerSp) {
        this.onAddClickListenerSp = onAddClickListenerSp;
    }

    @NonNull
    @Override
    public FriendAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_follow, parent, false);
        FriendAdapter.ViewHolder holder = new FriendAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull FriendAdapter.ViewHolder holder, int position) {
        Friend bean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + bean.getIcon())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.im1_follow);
        if (bean.getHost() == 1) {
            holder.im2_follow.setImageResource(R.mipmap.icon_ishostzhubo);
        } else {
            if (bean.getIdentity() == 10) {
                holder.im2_follow.setImageResource(R.mipmap.icon_huangjin);
            } else if (bean.getIdentity() == 20) {
                holder.im2_follow.setImageResource(R.mipmap.icon_bojin);
            } else if (bean.getIdentity() == 30) {
                holder.im2_follow.setImageResource(R.mipmap.icon_zuanshi);
            } else {
                holder.im2_follow.setVisibility(View.GONE);
            }
        }

        holder.tx1_follow.setText(bean.getName());
        holder.tx2_follow.setText(bean.getPersionSign());
        if (bean.getCall() == 1) {
            holder.re_dashan.setVisibility(View.GONE);
            holder.im_sixin.setVisibility(View.VISIBLE);
        } else {
            holder.re_dashan.setVisibility(View.VISIBLE);
            holder.im_sixin.setVisibility(View.GONE);
        }
        holder.im1_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(bean.getId());
            }
        });
        holder.re_dashan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClickDs.onItemClicksDs(bean.getId());
                bean.setCall(1);
                notifyItemChanged(position);
            }
        });
        holder.im_sixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClickSx.onItemClicksSx(bean.getId());
            }
        });
        holder.shipin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onAddClickListenerSp.onItemClicksSp(bean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im1_follow, im2_follow, im_sixin;
        private RelativeLayout re_dashan;
        private TextView tx1_follow, tx2_follow;
        private ImageView shipin;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            re_dashan = itemView.findViewById(R.id.re_dashan);
            im_sixin = itemView.findViewById(R.id.im_sixin);
            im1_follow = itemView.findViewById(R.id.im1_follow);
            im2_follow = itemView.findViewById(R.id.im2_follow);
            tx1_follow = itemView.findViewById(R.id.tx1_follow);
            tx2_follow = itemView.findViewById(R.id.tx2_follow);
            shipin = itemView.findViewById(R.id.shipin);
        }
    }
}
